package com.leosoft.heatmonitor.control;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.leosoft.heatmonitor.service.transactional.DataService;

@Controller
@RequestMapping("/data")
public class DataController {
	@Autowired(required = true)
	DataService dataService;

	String[] columns = { "", "projectName", "deviceKey", "building", "door", "user", "createtime", "end", "projectId" };

	@RequestMapping(value = "/list")
	@ResponseBody
	public Map<String, Object> list(HttpServletRequest request) {
		String username = getUserName();
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");
		String startTime = new String();
		String endTime = new String();
		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();

		for (int i = 0; i < columns.length; i++) {
			String orderColumn = request.getParameter("order[" + i + "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] { columns[Integer.parseInt(orderColumn)], orderDir });
			}
		}

		for (int i = 0; i < columns.length; i++) {
			String searchable = request.getParameter("columns[" + i + "][searchable]");

			if (searchable.equals("true")) {
				if (i < 6 || i == 8) {
					String searchColumn = columns[i];
					String value = request.getParameter("columns[" + i + "][search][value]");
					if (!value.isEmpty()) {
						wherePart.add(new String[] { searchColumn, value });
					}
				}
				if (i == 6) {
					startTime = request.getParameter("columns[" + i + "][search][value]");
				}
				if (i == 7) {
					endTime = request.getParameter("columns[" + i + "][search][value]");
				}

			}
		}

		Map<String, Object> result = dataService.list(startIndex, length, wherePart, odrerPart, startTime, endTime,
				username);
		result.put("draw", draw);
		return result;
	}

	@RequestMapping(value = "/monitorList")
	@ResponseBody
	public Map<String, Object> monitorList(HttpServletRequest request) {
		String[] columnsMonitor = { "", "projectName", "deviceKey", "building", "door", "user", "fee", "createtime",
				"end", "projectId" };
		String username = getUserName();
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");
		String startTime = new String();
		String endTime = new String();
		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();

		for (int i = 0; i < columnsMonitor.length; i++) {
			String orderColumn = request.getParameter("order[" + i + "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] { columnsMonitor[Integer.parseInt(orderColumn)], orderDir });
			}
		}

		for (int i = 0; i < columnsMonitor.length; i++) {
			String searchable = request.getParameter("columns[" + i + "][searchable]");

			if (searchable.equals("true")) {
				if (i < 7 || i == 9) {
					String searchColumn = columnsMonitor[i];
					String value = request.getParameter("columns[" + i + "][search][value]");
					if (!value.isEmpty()) {
						wherePart.add(new String[] { searchColumn, value });
					}
				}
				if (i == 7) {
					startTime = request.getParameter("columns[" + i + "][search][value]");
				}
				if (i == 8) {
					endTime = request.getParameter("columns[" + i + "][search][value]");
				}

			}
		}

		Map<String, Object> result = dataService.monitorList(startIndex, length, wherePart, odrerPart, startTime,
				endTime, username);
		result.put("draw", draw);
		return result;
	}

	String getUserName() {
		Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		String username;
		if (principal instanceof UserDetails) {
			username = ((UserDetails) principal).getUsername();
		} else {
			username = principal.toString();
		}
		return username;
	}

	@RequestMapping(value = "/heatTrends")
	@ResponseBody
	List<Map<String, String>> heatTrends(@RequestParam("projectId") String projectId,
			@RequestParam("start") String start, @RequestParam("end") String end) {
		String username = getUserName();
		return dataService.trends(projectId, start, end, username, "currentHeat");
	}

	@RequestMapping(value = "/areaTrends")
	@ResponseBody
	List<Map<String, String>> areaTrends(@RequestParam("projectId") String projectId,
			@RequestParam("start") String start, @RequestParam("end") String end) {
		String username = getUserName();
		return dataService.trends(projectId, start, end, username, "area");
	}

	@RequestMapping(value = "/periodTrends")
	@ResponseBody
	List<Map<String, String>> periodTrends(@RequestParam("projectId") String projectId,
			@RequestParam("start") String start, @RequestParam("end") String end) {
		String username = getUserName();
		return dataService.trends(projectId, start, end, username, "currentPeriod");
	}

	@RequestMapping(value = "/heatDArea")
	@ResponseBody
	List<Map<String, String>> heatDArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building, @RequestParam("door") String door) {
		String username = getUserName();
		return dataService.dArea(projectId, building, door, username, "totalHeat");
	}

	@RequestMapping(value = "/areaDArea")
	@ResponseBody
	List<Map<String, String>> areaDArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building, @RequestParam("door") String door) {
		String username = getUserName();
		return dataService.dArea(projectId, building, door, username, "area");
	}

	@RequestMapping(value = "/periodDArea")
	@ResponseBody
	List<Map<String, String>> periodDArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building, @RequestParam("door") String door) {
		String username = getUserName();
		return dataService.dArea(projectId, building, door, username, "totalPeriod");
	}

	@RequestMapping(value = "/temperatureDArea")
	@ResponseBody
	List<List<Map<String, String>>> temperatureDArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building, @RequestParam("door") String door) {
		String username = getUserName();
		return dataService.temperatureDArea(projectId, building, door, username);
	}

	@RequestMapping(value = "/heatArea")
	@ResponseBody
	List<Map<String, String>> heatArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building) {
		String username = getUserName();
		return dataService.area(projectId, building, username, "totalHeat");
	}

	@RequestMapping(value = "/areaArea")
	@ResponseBody
	List<Map<String, String>> areaArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building) {
		String username = getUserName();
		return dataService.area(projectId, building, username, "area");
	}

	@RequestMapping(value = "/periodArea")
	@ResponseBody
	List<Map<String, String>> periodArea(@RequestParam("projectId") String projectId,
			@RequestParam("building") String building, @RequestParam("door") String door) {
		String username = getUserName();
		return dataService.area(projectId, building, username, "totalPeriod");
	}

	@RequestMapping(value = "/dashBoard")
	@ResponseBody
	public Map<String, Object> dashBoard(HttpServletRequest request) {
		String[] dashBoardColumn = { "projectname", "heat", "m", "period", "temperature" };
		String username = getUserName();
		int startIndex = Integer.parseInt(request.getParameter("start"));
		int length = Integer.parseInt(request.getParameter("length"));
		String draw = request.getParameter("draw");
		List<String[]> odrerPart = new ArrayList<>();
		List<String[]> wherePart = new ArrayList<>();

		for (int i = 0; i < dashBoardColumn.length; i++) {
			String orderColumn = request.getParameter("order[" + i + "][column]");
			if (orderColumn != null) {
				String orderDir = request.getParameter("order[" + i + "][dir]");
				odrerPart.add(new String[] { dashBoardColumn[Integer.parseInt(orderColumn)], orderDir });
			}
		}

		for (int i = 0; i < dashBoardColumn.length; i++) {
			String searchable = request.getParameter("columns[" + i + "][searchable]");

			if (searchable.equals("true")) {

				String searchColumn = dashBoardColumn[i];
				String value = request.getParameter("columns[" + i + "][search][value]");
				if (!value.isEmpty()) {
					wherePart.add(new String[] { searchColumn, value });
				}

			}
		}
		Map<String, Object> result = dataService.dashBoard(startIndex, length, wherePart, odrerPart, username);
		result.put("draw", draw);
		return result;
	}
}
